//
//  MTCreatePartyJoinAmountCell.swift
//  MT
//
//  Created by ooops on 2020/5/24.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Eureka
import MTCategory
import GrowingTextView
import RxSwift
import RxCocoa

extension MTCreatePartyJoinAmountCell: MTAddFriendNumberViewDelegate {
    public func numberButtonResult(_ numberButton: MTAddFriendNumberView, number: String) {
        if let count = Int(number) {
            if count >= numberButton.maxValue {
                //MBProgressHUD_JChat.show(text: "已达最大礼物数量", view: self.view, 1.5)
                numberButton.textField.text = "\(numberButton.maxValue)"
                self.row.value = "\(numberButton.maxValue)"
            } else if count <= numberButton.minValue {
                numberButton.textField.text = "\(numberButton.minValue)"
                self.row.value = "\(numberButton.minValue)"
                //MBProgressHUD_JChat.show(text: "礼物数量不能小于\(numberButton.minValue)", view: self.view, 1.5)
            } else {
                self.row.value = number
            }
        }
    }
}

public class MTCreatePartyJoinAmountCell: Cell<String>, CellType {
    
    /// 显示模式
    lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCSemiboldFont(14)
        l.textColor = UIColor.text.x1d1a1a
        l.text = "名称"
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var numberView: MTAddFriendNumberView = {
        let v = MTAddFriendNumberView()
        v.setImage(decreaseImage: UIImage(named: "icon_gift_reduce")!, increaseImage: UIImage(named: "icon_gift_plus")!)
        v.textField.backgroundColor = .white
        v.textField.layer.borderColor = UIColor(hex: 0xd8d8d8).cgColor
        v.textField.layer.borderWidth = 1
        v.textField.layer.cornerRadius = 4
        v.textField.layer.masksToBounds = true
        v.shakeAnimation = true
        v.maxValue = 1000000
        v.minValue = 2
        v.delegate = self
        v.textField.endEditing(true)
        return v
    }()
    
    lazy var numberLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = UIColor.text.x9b9b9b
        l.textAlignment = .right
        l.isHidden = true
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    public var tipString: String? = nil
    
    private var isHiddenLineView: Bool = true {
        didSet {
            lineView.isHidden = isHiddenLineView
        }
    }
    
    public var disableNumberView: Bool = false {
        didSet {
            numberView.isHidden = disableNumberView
            numberLabel.isHidden = !disableNumberView
        }
    }
    
    open override func cellCanBecomeFirstResponder() -> Bool {
        return false
    }

    open override func cellBecomeFirstResponder(withDirection direction: Direction) -> Bool {
        return false
    }

    open override func cellResignFirstResponder() -> Bool {
        return false
    }
    
    
    override public func setup() {
        super.setup()
        self.backgroundColor = UIColor.theme.background
        self.selectionStyle = .none
        self.textLabel?.isHidden = true
        self.detailTextLabel?.isHidden = true
        self.height = { UITableView.automaticDimension }
        setupUI()
        makeConstraint()
        if row.value != nil {
            numberView.textField.text = row.displayValueFor?(row.value)
        }
    }
    
    
    override public func update() {
        super.update()
        tipLabel.text = tipString
        if row.value != nil {
            
        }
    }
}

// MARK: - 布局相关
extension MTCreatePartyJoinAmountCell {
    
    private func setupUI() {
        contentView.addSubview(tipLabel)
        contentView.addSubview(lineView)
        contentView.addSubview(numberView)
        contentView.addSubview(numberLabel)
    }
    
    private func makeConstraint() {
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
        }
        
        numberView.snp.makeConstraints { (make) in
            make.centerY.equalTo(tipLabel)
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(180)
            make.height.equalTo(30)
        }
        
        numberLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(tipLabel)
            make.right.equalToSuperview().offset(-32)
            make.width.equalTo(180)
            make.height.equalTo(30)
        }
    }
}

// MARK: - 文本编辑相关
extension MTCreatePartyJoinAmountCell {
    
    
}


// The custom Row also has the cell: CustomCell and its correspond value
public final class MTCreatePartyJoinAmountCellRow: Row<MTCreatePartyJoinAmountCell>, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
        cellProvider = CellProvider<MTCreatePartyJoinAmountCell>.init()
    }
}

